﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_Tower3._0
{
    struct Pos
    {
        public int x;
        public int y;
    }

    internal class Map
    {
        public enum Type
        {
            TrapWall = -1,
            None = 1,
            Wall = 0,
            Mon = 2,
            Tool = 3,
            Map1Step0 = 4,
            Spawn = 6,
            MonSlime = 7,
            MonSlime2 = 8,
            MonSlime3 = 9,
            MonSlime4 = 10,
            MonSlime5 = 11,
            MonSlime6 = 12,
            King = 13,
            Map1Key0 = 14,
            Map2Step0 = 15,
            Map2Button0  = 16,
            Map2Key0 = 17,
            Map2Key1 = 18,
            Map2Step1 = 19,
            MonGoblin = 20,
            MonGoblin2 = 21,
            MonGoblin3 = 22,
            MonGoblin4 = 23,
            MonGoblin5 = 24,
            MonGoblin6 = 25,
        };
        public struct Data
        {
            public Pos pos;
            public Type type;
        }

        public Data[,] data;
        public int currentIndex;
        public int width, height;
        string[] paths;
        Pos heroPos;

        public Map(string[] paths, int width, int height)
        {
            this.width = width;
            this.height = height;
            this.paths = paths;

            data = new Data[paths.Length, width * height];

            for (int i = 0; i < paths.Length; i++)
            {
                string temp = DataSystem.Instance.LoadData(paths[i]);

                string[] lines = temp.Split('\n');
                for (int j = 0; j < height; j++)
                {
                    string[] elements = lines[j].Split(',');
                    for (int k = 0; k < width; k++)
                    {
                        //序号 = 行号 * 列数 + 列号
                        int index = j * width + k;
                        data[i, index].pos.x = k;
                        data[i, index].pos.y = j;
                        data[i, index].type = (Type)int.Parse(elements[k]);
                        if (data[i, index].type == Type.Spawn)
                        {
                            data[i, index].type = Type.None;
                            Hero.pos.x = k;
                            Hero.pos.y = j;
                        }
                    }
                }
            }
        }

        public void Init()
        {
            for (int i = 0; i < paths.Length; i++)
            {
                string temp = DataSystem.Instance.LoadData(paths[i]);

                string[] lines = temp.Split('\n');
                for (int j = 0; j < height; j++)
                {
                    string[] elements = lines[j].Split(',');
                    for (int k = 0; k < width; k++)
                    {
                        //序号 = 行号 * 列数 + 列号
                        int index = j * width + k;
                        data[i, index].pos.x = k;
                        data[i, index].pos.y = j;
                        data[i, index].type = (Type)int.Parse(elements[k]);
                        if (data[i, index].type == Type.Spawn)
                        {
                            data[i, index].type = Type.None;
                            Hero.pos.x = k;
                            Hero.pos.y = j;
                        }
                    }
                }
            }
        }

        public void Render()
        {
            for (int i = 0; i < data.GetLength(1); i++)
            {
                switch (data[currentIndex, i].type)
                {
                    case Type.None:
                        break;
                    case Type.Wall:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y,ConsoleColor.Gray,ConsoleColor.Gray);
                        break;
                    case Type.TrapWall:
                        Utility.MyPrint("I", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Yellow, ConsoleColor.Red);
                        break;
                    case Type.Map1Step0:
                        Utility.MyPrint("L", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.White);
                        break;
                    case Type.Map2Step0:
                        Utility.MyPrint("L", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.White);
                        break;
                    case Type.Map2Step1:
                        Utility.MyPrint("L", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;
                    case Type.Spawn:
                        Utility.MyPrint("^", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Gray);
                        break;
                    case Type.Tool:
                        Utility.MyPrint("+", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Red);
                        break;
                    case Type.King:
                        Utility.MyPrint("!", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Yellow);
                        break;
                    case Type.Map1Key0:
                        Utility.MyPrint("Ī", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Yellow);
                        break;
                    case Type.Map2Key0:
                        Utility.MyPrint("Ī", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Yellow);
                        break;
                    case Type.Map2Key1:
                        Utility.MyPrint("Ī", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Yellow);
                        break;
                    case Type.Map2Button0:
                        Utility.MyPrint(".", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.DarkBlue);
                        break;
                    case Type.MonSlime:
                        Utility.MyPrint("O", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;
                    case Type.MonSlime2:
                        Utility.MyPrint("O", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;
                    case Type.MonSlime3:                                                                           
                        Utility.MyPrint("O", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;                                                                                     
                    case Type.MonSlime4:                                                                           
                        Utility.MyPrint("O", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;                                                                                     
                    case Type.MonSlime5:                                                                           
                        Utility.MyPrint("O", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;                                                                                     
                    case Type.MonSlime6:                                                                           
                        Utility.MyPrint("O", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Blue);
                        break;    
                    case Type.MonGoblin:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Green);
                        break;
                    case Type.MonGoblin2:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Green);
                        break;
                    case Type.MonGoblin3:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Green);
                        break;
                    case Type.MonGoblin4:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Green);
                        break;
                    case Type.MonGoblin5:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Green);
                        break;
                    case Type.MonGoblin6:
                        Utility.MyPrint("X", data[currentIndex, i].pos.x, data[currentIndex, i].pos.y, ConsoleColor.Green);
                        break;
                }
            }
        }

        public void ChangeMap(int index)
        {
            if (index < 0 || index >= data.GetLength(0))
            {
                Console.WriteLine("地图不存在！");
                return;
            }

            currentIndex = index;
            Console.Clear();
        }

        public Type GetType(Pos pos)
        {
            int index = pos.y * width + pos.x;
            return data[currentIndex, index].type;
        }
    }
}
